// pages/jilu/jilu.js
const recorderManager = wx.getRecorderManager()
const app=getApp();
const myaudio = wx.createInnerAudioContext();
Page({
   
    /**
     * 页面的初始数据
     */
    data: {
        ifselect:false,
        xinqing:'您的心情是',
        color:["#d78763","#e69dea","#99d38f","#8ed7c6",""],
        textcolor:"#cccccc",
        inputVal:"",
        value:"",
        isplay:false,
        text:"",
        Long_press:"长按录音",
        is_clock:false,
        hiddenmodalput:true,
        anyimage:"/images/voice(2).png",
        
        weather:[],
        care:[{
            "name":"开心",
            "color":"#d78763"
        },
        {
            "name":"难过",
            "color":"#e69dea"
        },
        {
            "name":"一般",
            "color":"#99d38f"
        },
        {
            "name":"愤怒",
            "color":"#8ed7c6"
        },
        {
            "name":"郁闷",
            "color":"#acc8ae"
        },
        {
            "name":"以上心情没有合适的",
            "color":"#f86a43"
        }
    ],
    
    
  
    },
    

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
       
        let now = new Date();
        let year = now.getFullYear();
        let month = now.getMonth() + 1;
        var that = this;
        let latitude;
        let longitude;
        this.setData({
            year: year,
            month: month,
            isToday: year +'年'+ month +"月"+ now.getDate()+"日"
        })
        
        
        wx.getLocation({
            
          isHighAccuracy:true,
          success(res){
            
              
                latitude=res.latitude
                longitude=res.longitude
                that.getCity(latitude,longitude)
                
          }
        })
        
        
        
    },
    keji(e){
        this.setData({
            text1:e.data.now.text
        })
        
    },
    getCity(latitude,longitude){
        var that = this;
       let weizhi =longitude+","+latitude
       
       wx.request({
            url:"https://devapi.qweather.com/v7/weather/now?key=e294f967ee0440699e27ee0fbbca6b65&location="+weizhi,
            data:{},
            method:"GET",
            
            success:function(res){
                that.keji(res);
               
                
            }
            
        })
        
        
    },
    
    
    listall(){
        this.setData({
            ifselect:!this.data.ifselect
        })
    },
    modalinput(){
        this.setData({
            hiddenmodalput:!this.data.hiddenmodalput
        })
    },
    Select(e){
        if(e.currentTarget.dataset.name!="以上心情没有合适的"){
            var name = e.currentTarget.dataset.name
            var color = e.currentTarget.dataset.color
            this.setData({
                xinqing:name,
                ifselect:false,
                textcolor:color

            })
        }else{
            this.setData({
                hiddenmodalput:!this.data.hiddenmodalput
            })
        }
        
    },
    deletetext(){
        
        this.setData({
            inputVal:this.data.inputVal.substring(0,this.data.inputVal.length-1)
        })
    },
    confirm(){
        if(this.data.value){
            this.setData({
                xinqing:this.data.value,
                value:"",
                ifselect:false,
                textcolor:"#fbc02d",
                hiddenmodalput:!this.data.hiddenmodalput
            })
        }else{
            wx.showToast({
                title:'不可为空',
                icon:'none',
                duration:1000
                })
        }
        

       
    },
    saveVal2(e){
        
        this.setData({
            text:e.detail.value
        })
        
    },

    saveVal1(e){
        
        this.setData({
            value:e.detail.value
        })
        
    },
    saveVal(e){
        this.setData({
            inputVal:e.detail.value
        })
    },
    plustext(){
        this.setData({
            text:this.data.inputVal,
            textcolor1:"black",
            inputVal:""
        })
    },
    alldelete(){
        this.setData({
            text:""
        })
    },
    handleRecordStart:function(e){
        this.setData({
            Long_press:"松开发送",
            is_clock:true,
            startPoint:e.touches[0],
            anyimage:"/images/voice1.png"
        })
        const options={
            duration:10000,
            sampleRate:16000,
            numberOfChannels:1,
            encodeBitRate:48000,
            format:'mp3'
        }
        recorderManager.start(options);
        wx.showToast({
          title: '正在录音，上划取消发送',
          icon:"none",
          duration:5000
        })
        console.log("zoule")
    },
  handleRecordStop:function(e){
      let that=this;
      recorderManager.stop()
      this.setData({
        Long_press:"长按录音",
        anyimage:"/images/voice(2).png"
      })
      console.log(this.data.is_clock)
      recorderManager.onStop((res)=>{
          if(this.data.is_clock==true){
              if(res.duration<2000){
                wx.showToast({
                title:'录音时间太短',
                icon:'none',
                duration:1000
                })
               }else{
                    const {tempFilePath} = res;
                    console.log(tempFilePath)
                    wx.showLoading({
                        title: '语音上传中',
                    })
                    setTimeout(function () {
                        wx.hideLoading()
                    }, 2000)
                    wx.uploadFile({
                        url:"https://wx.xxmtcds.top:2333/voice",
                        filePath:tempFilePath,
                        name:"voice",
                        success:function(res){
                            that.save(res)
                           
                           
                            
                            },
                            fail:function(res){
                                console.log("222")
                                
                         
                              },
                        })
                        
                       
                    }
                }else{
                    wx.showToast({
                        title:'取消录音成功',
                        icon:'none',
                        duration:1000
                    })
                }
               
            })      
        
          
      
  },
  save(res){
    this.setData({
        voiceUrl:res.data
    })
    
  },
  handleTouchMove:function(e){
    if(Math.abs(e.touches[e.touches.length-1].clientY-this.data.startPoint.clientY)>25){
        this.setData({
            is_clock:false
        })
        wx.showToast({
            title:"松开手指，取消发送",
            icon:"none",
            duration:1000
        })
       
    }
    
  },
  
  control(){
    myaudio.src=this.data.voiceUrl
    console.log(this.data.voiceUrl)
    if(this.data.isplay==false){
        myaudio.play();
        console.log(myaudio.duration)
        this.setData({
            isplay:true
        })
        
    }else{
        myaudio.pause();
        this.setData({
            isplay:false
        })
    }
  },
 

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        
       
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})